<template>
  <div class="login-container">
    <div class="title-container">
      <img src="http://likede2-admin.itheima.net/img/logo.595745bd.png" alt="" srcset="">
    </div>
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="请输入用户名"
          name="loginName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="code"
          v-model="loginForm.code"

          placeholder="请输入验证码"
          name="code"
          type="text"
          tabindex="3"
          auto-complete="on"
          style="width:280px"
        />
        <img :src="`http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/${loginForm.clientToken}`" alt="" srcset="" style="float:right ">
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: admin</span>
      </div>

    </el-form>
  </div>
</template>

<script>
// import { PictureVerification } from '@/api'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: '',
        loginType: 0
      },

      loginRules: {
        loginName: [{ required: true, trigger: 'blur', message: '请输入您的用户名' },
          { pattern: /^[a-zA-Z0-9]{5}$/, message: '请输入正确格式的用户名', trigger: 'blur' }
        ],
        password: [{ required: true, trigger: 'blur', message: '请设置密码' },
          { min: 5, max: 16, message: '请输入5-16位的密码', trigger: 'blur' }
        ],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      Imgurl: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  async created() {
    this.loginForm.clientToken = this.setSessionid()
    // clientToken

    // const res = await PictureVerification(this.loginForm.clientToken)
    // console.log(res)
    // this.Imgurl = res.config.url
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    setSessionid(num) {
      var len = num || 32
      var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789'
      var maxPos = chars.length
      var pwd = ''
      for (var i = 0; i < len; i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos))
      }
      return pwd
    },
    handleLogin() {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) return
        try {
          // 2. 发起请求
          this.loading = true
          const res = await this.$store.dispatch('user/loginApi', this.loginForm)
          console.log(res)
          // 登录成功，提示用户
          this.$message.success('登陆成功')
          // 3. TODO：存储 token
          // 4. 跳转到后台主页
          this.$router.push('/')
          this.loading = false
        } catch (err) {
          console.log(err)

          this.loading = false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(1, 1, 1, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>

$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
background: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2F25%2F03%2F2c%2F25032cd28554aacf564440b6b840b905.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668256698&t=e56d7f69ae630615e55e2813b31c188e') no-repeat;
background-size: 100% 100%;
height: 100%;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 60px 35px 0;
    margin: 0 auto;
margin-top: -50px;
    overflow: hidden;
    background-color: rgb(0, 0, 0,0.6);
    border-radius: 10px;
position: relative;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
margin: 0 auto;
margin-top: 150px;
position: relative;
z-index: 11;
width: 100px;
height: 100px;

img{
  width: 100%;
display: block;
position: relative;

}

  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
